package cn.leedsoft.and.crimeintent.common;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Display;
import android.widget.ImageView;

/**
 * Created by leeds on 2016/8/18 17:42.
 */
public class PictureUtil {
    private Activity mActivity;

    public PictureUtil(Activity activity) {
        mActivity = activity;
    }

    @SuppressWarnings("deprecation")
    public BitmapDrawable getScaledDrawable(String path) {

        return null;
    }

    @SuppressWarnings("deprecation")
    public void showPic(ImageView imageView, String path) {// TODO: 2016/8/22 不能按照imageView的尺寸显示。可能会报 divide by zero 异常
        if (path == null) return;
        // Resize the full image to fit in out image view.
        Display display = mActivity.getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();

        BitmapFactory.Options factoryOptions = new BitmapFactory.Options();

        factoryOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, factoryOptions);

        int imageWidth = factoryOptions.outWidth;
        int imageHeight = factoryOptions.outHeight;

        // Determine how much to scale down the image
        int scaleFactor = Math.min(imageWidth / width, imageHeight
                / height);

        // Decode the image file into a Bitmap sized to fill the
        // View
        factoryOptions.inJustDecodeBounds = false;
        factoryOptions.inSampleSize = scaleFactor;
        factoryOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(path,
                factoryOptions);
        imageView.setImageBitmap(bitmap);
    }

    public void clearPic(ImageView imageView) {
        if (imageView.getDrawable() == null ||
                imageView.getDrawable() == null ||
                ((BitmapDrawable) imageView.getDrawable()).getBitmap() == null) return;
        ((BitmapDrawable) imageView.getDrawable()).getBitmap().recycle();
        imageView.setImageDrawable(null);
        Log.d("PictureUtil", ">>>>>>>>>>>>bitmap is cleared!");
    }
}
